package com.tueye.utils;

import java.net.URL;

/**
 * 提供相关项目应用程序使用的方法
 * @author hanxiaowei
 * 
 */
public class AppHelper {
	
	public static String getWebAppPath() {
		AppHelper util = new AppHelper();
		String osname = System.getProperty("os.name");
		String clazzFilePath = util.getClassFilePath();
		String webPath = clazzFilePath.substring(0, clazzFilePath
				.indexOf("WEB-INF"));

		if (webPath.indexOf("file:") != -1) {
			webPath = webPath.substring(webPath.indexOf("file:") + 6, webPath
					.length());
		}
		if ((osname.indexOf("Unix") != -1) || (osname.indexOf("unix") != -1)) {
			webPath = "//" + webPath;
		}
		if ((osname.indexOf("Linux") != -1) || (osname.indexOf("linux") != -1)) {
			webPath = "//" + webPath;
		}
		return webPath;

	}
	/**
	 * 获取classes文件的绝对路径

	 * 
	 * @return String
	 */
	private String getClassFilePath() {
		String strClassName = getClass().getName();
		String strPackageName = "";
		if (getClass().getPackage() != null) {
			strPackageName = getClass().getPackage().getName();
		}
		String strClassFileName = "";
		if (!"".equals(strPackageName)) {
			strClassFileName = strClassName.substring(
					strPackageName.length() + 1, strClassName.length());
		} else {
			strClassFileName = strClassName;
		}
		URL url = null;
		url = getClass().getResource(strClassFileName + ".class");

		String strURL = url.getFile();
		try {
			strURL = java.net.URLDecoder.decode(strURL, "UTF-8");
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return strURL;
	}
}
